/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 */
package seedpod.webapp.view.seedpodwidgets;

/**
 * This generic widget will try to print anything as long as the object has a toString() function implemented.
 */

import java.util.ArrayList;

import seedpod.exceptions.DataValidationException;
import seedpod.exceptions.InvalidWidgetException;
import seedpod.kb2db.SlotMap;
import seedpod.model.AVPair;
import seedpod.model.SeedpodDO;
import seedpod.model.SeedpodModel.RdbCls.FormWidget;
import seedpod.model.SeedpodModel.RdbCls.ViewWidget;
import seedpod.webapp.Seedpod;
import seedpod.webapp.view.htmlwidget.GenericHtmlWidget;
import seedpod.webapp.view.htmlwidget.ImageButton;

public class LimsWidget extends GenericHtmlWidget {
	/**
	 * Each widget has a label, a value, and an EMSattribute definition
	 */
	// protected SlotMap _attribute = null;
	// protected boolean _isValidWidget = false;
	// protected Object _data = null;
	protected AVPair _avPair = null;

	public LimsWidget() {
	// TODO Auto-generated constructor stub
	}

	public LimsWidget(AVPair avPair) {
		setAVPair( avPair );
		setId( avPair.genRefId() );
	}

	public boolean allowInPlaceEdit() {
		return true;
	}

	public String doEdit() {
		if ( getId().length() == 0 ) setId( getDefaultID() );
		if ( getData() == null ) setData( _avPair.getValue() );
		return render();
	}

	/**
	 * Assigns input value to the correct AVPair in the object. Depending on the widget, it takes care of the correct
	 * casting if possible.
	 * 
	 * @param obj
	 * @param input
	 */
	public void doPost( SeedpodDO obj, Object input ) {
		AVPair avPair = obj.getAVPair( getAttribute() );
		avPair.addValue( input.toString() );
	}

	public String doView() {
		if ( getId().length() == 0 ) setId( getDefaultID() );
		if ( getData() == null ) setData( _avPair.getValue() );
		return render();
	}

	public SlotMap getAttribute() {
		return _avPair == null ? null : _avPair.getAttribute();
	}

	public Object getData() {
		return _avPair == null ? "" : _avPair.getValue();
	}

	private String getDefaultID() {
		return getAttribute().getName() + "(0)";
	}

	public FormWidget getFormWidgetType() {
		return FormWidget.TEXT;
	}

	@Override
	public String getId() {
		return _avPair != null ? _avPair.genRefId() : getDefaultID();
	}

	public ViewWidget getViewWidgetType() {
		return ViewWidget.STRING;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String render() {
		Object data = getData();

		if ( data instanceof ArrayList ) {
			String result = "";
			ArrayList dataSet = (ArrayList)data;
			for ( Object item : dataSet )
				result += item.toString() + "<br>";
			return result;
		} else return data == null ? "" : getData().toString();
	}

	protected String renderErrorComment( String errMsg ) {
		String commentButtonUrl = Seedpod.getAbsoluteUrl( "/images/comment.gif" );
		ImageButton commentBtn = new ImageButton( "comment", commentButtonUrl, "#", "click for message" );
		commentBtn.addJavascript( "onclick=\"Effect.toggle('uneditable_" + getAttribute().getName() + "','appear'); return false;\" " );

		if ( errMsg == null || errMsg.length() == 0 )
			errMsg = "(E) An invalid widget was used or the data type is not supported by this widget.";
		String comment = "<span id=\"uneditable_" + getAttribute().getName() + "\" style=\"display:none; position:absolute;\">";
		comment += errMsg;
		comment += "</span>";

		return commentBtn.render();
	}

	private void setAVPair( AVPair avPair ) {
		_avPair = avPair;
	}

	public void setData( Object data ) {
		if ( _avPair != null ) _avPair.setValue( data );
	}

	public boolean supportsMultiValueInput() {
		return false;
	}

	/**
	 * Validate submission data to a form widget.
	 * 
	 * @param submittedValue
	 * @return
	 */
	public boolean validateSubmissionData( Object submittedValue ) throws DataValidationException {
		if (! getAttribute().isNullable() && submittedValue == null ) 
			throw new DataValidationException( getAttribute().getName() + " is a required field. "); 
		return true;
	}

	/**
	 * validate usage of the widget based on data type
	 */
	public void validateWidget() throws InvalidWidgetException {

	}
}
